package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    //分页查询
    @RequestMapping("/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = menuService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    //获取父菜单
    @RequestMapping("/findParent.do")
    public Result findParent (){

        List<Menu> menuList = null;
        try {
            menuList = menuService.findParent();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询父菜单失败");
        }
        return new Result(true,"查询父菜单成功",menuList);

    }

    //新增
    @RequestMapping("/add.do")
    public  Result add(@RequestBody Menu menu){

        try {
            Integer count = menuService.add(menu);
            if (count < 0){
                return new Result(false,MessageConstant.ADD_MENU_FAIL,"网络繁忙，请稍后再试");
            }
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL,e.getMessage());
        }
    }
    //编辑菜单
    @RequestMapping("/edit.do")
    public  Result edit(@RequestBody Menu menu){

        try {
            Integer count = menuService.edit(menu);
            if (count < 0){
                return new Result(false,MessageConstant.EDIT_MENU_FAIL,"网络繁忙，请稍后再试");
            }
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL,e.getMessage());
        }
    }

    @RequestMapping("/findById/{id}.do")
    public Result findById(@PathVariable Integer id) {
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,"查询数据成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询数据失败","网络繁忙，请稍后再试");
        }
    }
    //删除菜单 注意菜单关联的角色

    @RequestMapping("/deleteById/{id}.do")
    public Result deleteById(@PathVariable Integer id) {
        System.out.println("id = " + id);
        try {
            Integer count = menuService.deleteById(id);
            if (count < 0){
                return new Result(false, MessageConstant.DELETE_MENU_FAIL ,"网络繁忙，请稍后再试");
            }
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL,e.getMessage());
        }
    }

}
